package com.begood.leishi.fingerprint;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextClock;
import android.widget.TextView;
import android.widget.Toast;

import com.begood.leishi.fingerprinter.FingerPrinter;
import com.begood.leishi.fingerprinter.FpError;
import com.begood.leishi.fingerprinter.Mcro;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    FingerPrinter mFinger;
    Button openBtn,registerBtn,matchBtn,deleteBtn;
    EditText nameET;
    TextView Output;
    private ProgressDialog mProgressDialog;
    private Timer timerTask;

    private int registerCounter = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFinger = new FingerPrinter(this);

        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setCanceledOnTouchOutside(false);

        openBtn = (Button) findViewById(R.id.open_button);
        openBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                openBtn.setEnabled(false);
                if (openBtn.getText().toString().equals(getString(R.string.open_dev))) {
                    try {
                        mFinger.open(new FingerPrinter.openUsbListener() {
                            @Override
                            public void onSuccess() {
                                Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
                                openBtn.setText(getString(R.string.close_dev));
                                openBtn.setEnabled(true);
                            }

                            @Override
                            public void onFailure(String message) {
                                Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
                                openBtn.setText(getString(R.string.open_dev));
                                openBtn.setEnabled(true);
                            }
                        });
                    } catch (IOException e) {
                        openBtn.setEnabled(true);
                        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }else {
                    mFinger.close();
                    openBtn.setText(getString(R.string.open_dev));
                    openBtn.setEnabled(true);
                }
            }
        });
        nameET = (EditText) findViewById(R.id.user_name);

        registerBtn = (Button)findViewById(R.id.register_button);
        registerBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if(registerBtn.getText().toString().equals(getString(R.string.register_finger))) {
                    registerBtn.setText(getString(R.string.register_fingering));
                    registerBtn.setEnabled(false);
                    register();
                }
            }
        });
        Output = (TextView)findViewById(R.id.output);

        matchBtn = (Button)findViewById(R.id.match_button);
        matchBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if(matchBtn.getText().toString().equals(getString(R.string.match_finger))) {
                    matchBtn.setText(getString(R.string.match_fingering));
                    matchBtn.setEnabled(false);
                    match();
                }
            }
        });
        deleteBtn = (Button)findViewById(R.id.delete_button);
        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(deleteBtn.getText().toString().equals(getString(R.string.delete_finger))) {
                    deleteBtn.setText(getString(R.string.delete_fingering));
                    deleteBtn.setEnabled(false);
                    delete();
                }
            }
        });
    }
    private void register() {
        mFinger.register(new FingerPrinter.fingerListener() {
            @Override
            public void onSuccess(int value) {
                String outputText = "用户名"+mFinger.getNameByIDFromDB((long)value)+"注册成功，ID："+value;
                Output.setText(outputText);
                registerCounter = 0;
                registerBtn.setText(getString(R.string.register_finger));
                registerBtn.setEnabled(true);
            }
            @Override
            public void onNext(int status){
                if(status == Mcro.STATUS_NO_FINGER){
                    registerCounter++;
                    if(registerCounter == 1) {
                        String tempText = "请放入手指";
                        Output.setText(tempText);
                    }else if(registerCounter == 2) {
                        String tempText = "请再次放入手指";
                        Output.setText(tempText);
                        registerCounter = 0;
                    }
                }else if(status == Mcro.STATUS_REGISTER_AGAIN){
                    String tempText = "第一次录入成功,请拿起手指";
                    Output.setText(tempText);
                }
            }
            @Override
            public void onFailure(int errorCode) {
                registerBtn.setText(getString(R.string.register_finger));
                registerBtn.setEnabled(true);
                registerCounter = 0;
                if(errorCode == 0x21) {
                    Output.setText("没有检测到手指");
                }else {
                    String temp = FpError.message(errorCode);
                    Output.setText(temp);
                }
            }
        }, nameET.getText().toString().trim());
    }

    private void match(){
        mFinger.matchFinger(new FingerPrinter.fingerListener() {
            @Override
            public void onSuccess(int value) {
                String name = mFinger.getNameByIDFromDB((long)value);
                if(name != null){
                    String outputText = "用户名:"+name+"\tID："+value;
                    Output.setText(outputText);
                    matchBtn.setText(getString(R.string.match_finger));
                    matchBtn.setEnabled(true);
                }
            }
            @Override
            public void onNext(int status){
                if(status == Mcro.STATUS_NO_FINGER){
                    String tempText = "请放入手指";
                    Output.setText(tempText);
                }
            }
            @Override
            public void onFailure(int errorCode) {
                matchBtn.setText(getString(R.string.match_finger));
                matchBtn.setEnabled(true);
                if(errorCode == 0x21) {
                    Output.setText("没有检测到手指");
                }else {
                    String temp = FpError.message(errorCode);
                    Output.setText(temp);
                }
            }
        });
    }
    private void delete(){
        mFinger.deleteData(new FingerPrinter.fingerListener() {
            @Override
            public void onSuccess(int value) {
                deleteBtn.setText(getString(R.string.delete_finger));
                deleteBtn.setEnabled(true);
                Output.setText("清空数据成功");
            }
            @Override
            public void onNext(int status){

            }
            @Override
            public void onFailure(int errorCode) {
                deleteBtn.setText(getString(R.string.delete_finger));
                deleteBtn.setEnabled(true);
                String temp ="清空失败:"+ FpError.message(errorCode);
                Output.setText(temp);
            }
        });
    }
}
